package com.sczmgk.hotline_pzh.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.entity.InformationBean;
import com.sczmgk.hotline_pzh.entity.Picurls;
import com.sczmgk.hotline_pzh.utils.Tools;

import java.util.List;

import static com.sczmgk.hotline_pzh.MyApp.bitmapUtils;

/**
 * Created by Administrator on 2016/11/23 0023.
 */

public class MyNewsHomeAdapter extends BaseAdapter {

    private Context context;
    private List<InformationBean> lists;
    private final int screenWidth;


    public void setLists(List<InformationBean> lists) {
        this.lists = lists;
    }

    public List<InformationBean> getLists() {
        return lists;
    }

    public MyNewsHomeAdapter(Context context, List<InformationBean> lists) {
        this.lists = lists;
        this.context = context;
        screenWidth = Tools.getScreenWidth(context);
    }

    public MyNewsHomeAdapter(Context context) {
        this.context = context;
        screenWidth = Tools.getScreenWidth(context);
    }

    @Override
    public int getCount() {
        return lists == null ? 0 : lists.size();
    }


    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        int itemViewType = getItemViewType(position);

        InformationBean newsInfo = lists.get(position);

        if (itemViewType == 1) {

            //多图条目
            View photosView = View.inflate(context, R.layout.item_main_home_news_photos, null);

            TextView photosTitle = (TextView) photosView.findViewById(R.id.tv_news_photos_item_title);
            LinearLayout photosLayout = (LinearLayout) photosView.findViewById(R.id.ll_news_photos_item);
            TextView photosFrom = (TextView) photosView.findViewById(R.id.tv_news_photos_item_from);
            TextView photosDate = (TextView) photosView.findViewById(R.id.tv_news_photos_item_date);
            View viewLine = photosView.findViewById(R.id.view_line) ;

            if (position == lists.size() - 1){
                viewLine.setVisibility(View.GONE);
            }

            photosTitle.setText(newsInfo.title);
            photosFrom.setText(newsInfo.origin);
            photosDate.setText(newsInfo.publishtime);

            List<Picurls> picurls = newsInfo.picurls;

            if (picurls != null && picurls.size() > 0) {
                for (int i = 0; i < picurls.size(); i++) {
                    ImageView iv = new ImageView(context);
                    iv.setBackgroundResource(R.drawable.default_center);

                    bitmapUtils.display(iv, picurls.get(i).smallpic);

                    iv.setScaleType(ImageView.ScaleType.FIT_XY);

                    int imageWidth = (screenWidth - Tools.dip2px(context, 45)) / 3;
                    int imageHeight = Tools.dip2px(context, 75);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(imageWidth, imageHeight);

                    params.gravity = Gravity.CENTER;

                    if (i != 0) {
                        params.leftMargin = Tools.dip2px(context, 8);
                    }

                    iv.setLayoutParams(params);

                    photosLayout.addView(iv);
                }
            }
            return photosView;
        } else {

            //资讯条目
            View newsView = View.inflate(context, R.layout.main_news_item, null);

            final ImageView newsIvIcon = (ImageView) newsView.findViewById(R.id.iv_main_news_item_icon);
            TextView newsTvTitle = (TextView) newsView.findViewById(R.id.tv_main_news_item_title);
            TextView newsTvFrom = (TextView) newsView.findViewById(R.id.tv_main_news_item_from);
            TextView newsTvDate = (TextView) newsView.findViewById(R.id.tv_main_news_item_date);
            View viewLine = newsView.findViewById(R.id.view_line_single) ;

            //设置数据
            newsTvTitle.setText(newsInfo.title);
            newsTvFrom.setText(newsInfo.origin);
            newsTvDate.setText(newsInfo.publishtime);

            if (position == lists.size() - 1){
                viewLine.setVisibility(View.GONE);
            }

            List<Picurls> picurls = newsInfo.picurls;

            if (picurls != null && picurls.size() != 0) {
                for (int i = 0; i < picurls.size(); i++) {

                    bitmapUtils.display(newsIvIcon, picurls.get(i).smallpic);
                }
            } else {
                newsIvIcon.setImageResource(R.drawable.default_center);
            }
            return newsView;
        }

    }

    // 条目类型的总数
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    // 不同位置对应的条目类型是什么
    @Override
    public int getItemViewType(int position) {
        return lists.get(position).type;
    }


}



